﻿$(function() {
	bindEvent();
	tab();
	calendar();
	if(window.history && window.history.pushState) {  
    $(window).on('popstate', function() {  
        var hashLocation = location.hash;  
        var hashSplit = hashLocation.split("#!/");  
        var hashName = hashSplit[1];  
        if(hashName !== '') {  
            var hash = window.location.hash;  
            if(hash === '') {  
                 window.location.href='list.html';
            }  
        }  
    });  
    window.history.pushState('forward', null, 'list.html');  
}  

})

$(window).load(function(){
	$(".end").css("left","250px");
})
function bindEvent() {
	$(".list_navbar").on("click", "li", function() {
		$(this).find("div").addClass("select");
		$(this).siblings().find("div").removeClass("select");
	});
 	 var top = 0;
       
      
	$(".filter_cl").click(function() {
				
		$(".filter_select").fadeIn(200);
		$(".mask_filter").fadeIn(200);
		top = $(window).scrollTop();
           	$('body').css("top",-top+"px");
            	$('body').addClass('addbody');
		
	})
	//取消按钮
	$(".btns_cancel").click(function() {
		$(".filter_select").fadeOut(200);
		$(".mask_filter").fadeOut(200);
 		$('body').removeClass('addbody');
            	$(window).scrollTop(top);
	})
	$(".mask_filter").click(function() {
		$(".filter_select").fadeOut(200);
		$(".mask_filter").fadeOut(200);
		$('body').removeClass('addbody');
		$(window).scrollTop(top);
	})
	$(".btns_ok").click(function(){
		$(".filter_select").fadeOut(200);
		$(".mask_filter").fadeOut(200);
		$('body').removeClass('addbody');
		$(window).scrollTop(top);;
	})
	//最下方3个排序按钮
	$(".four_btns a").click(function(){
		if(!$(this).hasClass("filter_cl")){
			if(!$(this).hasClass("select")) {
				$(this).addClass("select").parent().siblings().find("a").removeClass("select");
			}
		}
	})
	var Timeoff = true,Price = true;
	$(".filter_w").click(function(){
		Price = true
		$(".filter_share").text("共享");
		$(this).find("i").removeClass("icon-time-f").addClass("icon-time-4b");
		if(Timeoff){
			$(this).find("span").text("出发晚-早");
			Timeoff = false;
		}else{
			$(this).find("span").text("出发早-晚");
			Timeoff = true;
		}
		$(".filter_price").find("span").text("票价");
		$(".filter_price").find("i").addClass("icon-price-f").removeClass("icon-price-4b");
	})
	$(".filter_price").click(function(){
		Timeoff = true
		$(this).find("i").removeClass("icon-price-f").addClass("icon-price-4b");
		if(Price){
			$(this).find("span").text("价格低-高");
			Price = false;
		}else{
			$(this).find("span").text("价格高-低");
			Price = true
		}
		$(".filter_w").find("span").text("时间");
		$(".filter_w").find("i").addClass("icon-time-f").removeClass("icon-time-4b");
	})
	/*$(".filter_price").click(function(){
		Share = true;
		Timeoff = true;
		$(".filter_share").text("共享");
		$(".filter_w").find("span").text("出发早-晚")
	})*/
	//筛选 左边及其切换
	$(".air_one").click(function() {
		var $this = $(this);
		iftab(".b_one",$this);
	});
	$(".air_two").click(function() {
		var $this = $(this);
		iftab(".b_two",$this);
	});
	$(".air_the").click(function() {
		var $this = $(this);
		iftab(".b_the",$this);
	});
	$(".air_f").click(function() {
		var $this = $(this);
		iftab(".b_f",$this);
	});
	$(".air_fi").click(function() {
		var $this = $(this);
		iftab(".b_fi",$this)
	});
	//-----------------time select---------------
	$(".air_time_solt").on("click", "input[type=button]", function() {
		if(!$(this).hasClass("select")) {
			$(this).addClass("select").siblings().removeClass("select");
		}
	});
	$(".air_time_solt input[type=button]:first-child").click(function(){
		$(".start").css("left","0px");
		$(".low").css("left","0px");
		$(".high").css("left","125px");
		$(".end").css("left","125px");
		$(".start").text("0");
		$(".end").text("12");
		$(".range-slider").val("0,12");
		$(".selected-bar").css({width:"154px",left:"2px"})
	})
	$(".air_time_solt input[type=button]:nth-child(2)").click(function(){
		$(".start").css("left","125px");
		$(".low").css("left","125px");
		$(".high").css("left","200px");
		$(".end").css("left","200px");
		$(".start").text("12");
		$(".end").text("18");
		$(".range-slider").val("12,18");
		$(".selected-bar").css({width:"83px",left:"147px"})
	})
	$(".air_time_solt input[type=button]:last-child").click(function(){
		$(".start").css("left","200px");
		$(".low").css("left","200px");
		$(".high").css("left","270px");
		$(".end").css("left","270px");
		$(".start").text("18");
		$(".end").text("24");
		$(".range-slider").val("18,24");
		$(".selected-bar").css({width:"77px",left:"223px"})
	})
	//---------------------------进度条--------------------
	$('.range-slider').jRange({
		from: 0,
		to: 24,
		step: 1,
		scale: [0, 24],
		format: '%s',
		width: 300,
		showLabels: true,
		showScale: false,
		isRange: true,
		dayOff:function(){
			var startime = $(".range-slider").val();
			var btn_mo = [0,12];
			var btn_lu = [12,18];
			var btn_ev = [18,24];
			var arr = startime.split(",");
			if(btn_mo[0] <= arr[0] && arr[0] <= btn_mo[1] && arr[1] >= btn_mo[0] && arr[1] <=  btn_mo[1]){
				
				$(".btns_time:first-child").addClass("select").siblings("input").removeClass("select");
				$(".btns_reset").addClass("c153");
				
			}else if(btn_lu[0] <= arr[0] && arr[0] <= btn_lu[1] && arr[1] >= btn_lu[0] && arr[1] <=  btn_lu[1]){
				
				$(".btns_time:nth-child(2)").addClass("select").siblings("input").removeClass("select");
				$(".btns_reset").addClass("c153");
				
			}else if(btn_ev[0] <= arr[0] && arr[0] <= btn_ev[1] && arr[1] >= btn_ev[0] && arr[1] <=  btn_ev[1]){
				
				$(".btns_time:last-child").addClass("select").siblings("input").removeClass("select");
				$(".btns_reset").addClass("c153");
			}else{
				$(".btns_time").removeClass("select");
				$(".btns_reset").addClass("c153");
			}
		}
	});
	//---------------------航空 公司-------------------------------
	$(".air_select_ul").on("click", "li", function() {
		if(!$(this).hasClass("select")) {
			$(this).addClass("select").siblings().removeClass("select");
		}
	});
	$(".airport_ul").on("click", "input", function() {
		$(this).parent().parent().parent().find(".limit").removeClass("select");
		if($(this).is(':checked')) {
			$(this).next().addClass("checkes");
		} else {
			$(this).next().removeClass("checkes");
		}
	});

	$(".airport_ul .limit").click(function() {
		if(!$(this).hasClass("select")) {
			$(this).addClass("select").siblings().children().find("div").removeClass("checkes");
		}
		$(this).siblings().children().find("input").attr('checked', false);
	});
	//-----------only-----直达---------
	$(".only").click(function() {
		
		if(!$(this).hasClass("nonstop")) {
			$(this).addClass("nonstop").removeClass("stop");
			$(this).children().removeClass("unselect").addClass("select");
		} else {
			$(this).removeClass("nonstop").addClass("stop");
			$(this).children().removeClass("select").addClass("unselect");
		}
	});
	//----监听筛选内容有无变化----------reset -------------
	$(".filter_right").click(function() {
		var sl ;
		var checklen = 0;
		var startime = $(".range-slider").val();
		var arr = startime.split(",");
		var stest ="0",etest = "24";
		var onlyVal;
		$(".airport_ul input[type=checkbox]").each(function() {
			if($(this).is(":checked")) {
				checklen++;
			}
		});
		$(".filter_right").each(function(){
			onlyVal = $(".only").hasClass("nonstop");
			var csel = $(".filter_right .select")
			sl = csel.length;
		})
		if(sl > 0 || checklen > 0 ) {
			$(".btns_reset").addClass("c153")
		} else if(sl == 0 && checklen == 0 ) {
			$(".btns_reset").removeClass("c153")
		}else if(arr[0]!= stest || arr[1] != etest) {
			$(".btns_reset").addClass("c153");
		}else if(onlyVal == true){
			$(".btns_reset").addClass("c153")
		}else if(onlyVal == false){
			$(".btns_reset").removeClass("c153")
		}

	})
	//恢复默认设置
	$(".btns_reset").click(function() {
		$(".filter_right .select").removeClass("select");
		$(".only").removeClass("nonstop").addClass("stop");
		$(".only").children().removeClass("select").addClass("unselect");
		$(".airport_ul input[type=checkbox]").attr('checked', false);
		$(".airport_ul input").next().removeClass("checkes");
		$(".start").css("left","0px");
		$(".low").css("left","0px");
		$(".high").css("left","280px");
		$(".end").css("left","270px");
		$(".start").text("0");
		$(".end").text("24");
		$(".selected-bar").css({width:"294px",left:"6px"})
		$(".btns_reset").removeClass("c153");
	})
	//日历弹出
	$(".list_nav .calendar").click(function(){
		$(".air-content").addClass("none")
		$(".date_main").removeClass("none");
	})
	$(".date_main").on("click","li",function(){
		$(".date_main").addClass("none");
		$(".air-content").removeClass("none");
	})
	/*---calendar select---*/
	$(".date_box").on("click","li",function(){
		if(!$(this).hasClass("unselect")){
			$(".month_date").children(".select").removeClass("select")
			$(this).addClass("select");
		}
	})
}
/*筛选tab切换*/
function iftab(obj,ethis) {
	$(obj).removeClass("none").siblings().addClass("none");
	$(ethis).addClass("select").siblings().removeClass("select");
	$(".only_btns").removeClass("none");
}
/**滚来滚去当前date **/
function tab(date) {
	var aDate = new Date();
	date = date || (aDate.getFullYear() + "-" + (aDate.getMonth() + 1) + "-" + aDate.getDate());
	var arr = date.split('-');
	var week = new Array("周日", "周一", "周二", "周三", "周四", "周五", "周六");
	var lit = " ",liselect = " ";
	var upNum, caseNum;
	var ifday = parseInt(arr[2]) - parseInt(aDate.getDate());
	if(ifday < 7) {
		upNum = parseInt(aDate.getDate());
		caseNum = parseInt(arr[2]) + 15;
	} else if(ifday >= 7) {
		upNum = parseInt(arr[2]) - 7;
		caseNum = parseInt(arr[2]) + 7;
	}
	for(var i = upNum; i < caseNum; i++) {
		var newDate = new Date(arr[0], arr[1] - 1, i);
		var nowDate = newDate.getDate();
		var nowYear = newDate.getFullYear();
		var nowMonth = newDate.getMonth() + 1;
		var nowWeek = "";
		var liHtml = $(".swiper-wrapper");
		var Month_day = String(nowMonth + "-" + nowDate);
		if(nowYear == aDate.getFullYear() && nowMonth == (aDate.getMonth() + 1) && nowDate == aDate.getDate()) {
			nowWeek = "今天";
		} else if(nowYear == aDate.getFullYear() && nowMonth == (aDate.getMonth() + 1) && nowDate == (aDate.getDate() + 1)) {
			nowWeek = "明天";
		} else {
			nowWeek = week[newDate.getDay()];
		}
		if(arr[0] == nowYear && arr[1] == nowMonth && arr[2] == nowDate) {
			liselect = "select";
		} else {
			liselect = " ";
		}
		//机票价格在这里，这里获取每日机票价格
		lit = lit + "<li class='swiper-slide' daynum ='" + nowYear + "-" + Month_day + "'><div class='list_time " + liselect + "'>" +
			"<label class='f_24'>" + nowWeek + "</label>" +
			"<label class='f_20 day'>" + Month_day + "</label>" +
			"<label class='f_24'>" + "¥1210" + "</label></div></li>";
	}
	liHtml.html(lit);
	$(".swiper-wrapper li").css("width", parseFloat($(".list_navbar").css("width")) / 5 + "px");
	liHtml.css("width", parseFloat($(".list_navbar").css("width")) / 5 * 15 + "px");
	var swiper = new Swiper('.swiper-container', {
		slidesPerView: 5,
		spaceBetween: 0,
	});
}

function calendar(date){
	var aDate = new Date();
	date = date || (aDate.getFullYear()+"-"+(aDate.getMonth()+1)+"-"+aDate.getDate());
	var arr = date.split('-');
	showDate(arr[0],arr[1],4)
}

//年月写入日历  !!!
    function showDate(yyyy,mm,monthsize){
        var daysCount;    
		monthsize = monthsize || 2;
        //本月
        var tyear= new Date().getFullYear();
        var tmonth = new Date().getMonth()+1;
		var tday = new Date().getDate();
        var titleHtml = " ",Datebox = $(".date_box");
		
        for( i = 0 ;i < monthsize;i++){
			var newDate =new Date(parseInt(yyyy),parseInt(mm)+i,0);
			var nowDate = newDate.getDate();
			var nowMonth = newDate.getMonth()+1;
			var nowYear = newDate.getFullYear();
			var week = new Date(Date.UTC(parseInt(yyyy),(parseInt(mm)+i)-1, 1)).getDay();;
			var mystr ="",setcolor = " ",unable = " ";
			daysCount = nowDate;
			for(k=0;k<daysCount;k++){
				var dayweek = new Date(Date.UTC(parseInt(yyyy),(parseInt(mm)+i)-1, k+1)).getDay()
            //计算格子
				if( k%7 == 0){
					if(k<7){
						for(j=0;j<week;j++){
							mystr += "<li class='unselect'></li>";
						}
					}
				}
				if(dayweek== 0 || dayweek == 6){
					setcolor = "week"
				}else{
					setcolor = "";
				}
				
				if(tyear == nowYear && tmonth == nowMonth && tday >= k+1){
					unable = "unselect";
					setcolor = "";
				}else{
					unable = " ";
				}
				if(tyear == nowYear && tmonth == nowMonth && tday == k+1){
					mystr += "<li  class='"+unable+"'><label></label><label class='"+setcolor+" Month_day'>今天</label><label class='Month_money'> </label></li>"; 
				}else{
					//这里一天，修改价格 填充内容在此
					mystr += "<li  class='"+unable+"'><label></label><label class='"+setcolor+" Month_day'>"+(k+1)+"</label><label class='Month_money'> </label></li>"; 
				}
			}
			//这里一个月的内容
			titleHtml = titleHtml+'<div  class="date_time" >'
								+'<div class="month">'+ nowYear+"年"+nowMonth+"月"+'</div>'
								+'<ul class="month_date">'+mystr+'</ul>'
								+'</div>'
		}
        Datebox.html(titleHtml);
}
